﻿package we3d.renderer.wireframe 
{
	import flash.display.BitmapData;
	
	import we3d.View3d;
	import we3d.core.Camera3d;
	import we3d.scene.SceneObject;
	import we3d.mesh.Vertex;
	import we3d.mesh.Face;
	import we3d.math.Matrix3d;
	import we3d.renderer.IRenderer;
	import we3d.rasterizer.Rasterizer;
	import we3d.material.FlatAttributes;
	
	/**
	* Polygon Culling against the camera frustum
	*/
	public class WFNone implements IRenderer 
	{
		public function WFNone () {}
		
		public function draw (view:View3d) :void {
			
			var v:Camera3d = view.scene.cam;
			var objectList:Array = view.scene.objectList;
			var f:Number = view.scene.currentFrame;
			
			view.initFrame(f);
			
			var objs:int = objectList.length;
			var cgv:Matrix3d = new Matrix3d();
			
			var pL:int;
			var i:int;
			var j:int;
			var k:int;
			var L:int;
			var r:Array;
			var vtxs:Array;
			var vtxa:Vertex;	
			var vtxb:Vertex;
			var p:Face;
			var o:SceneObject;
			
			var sl:Rasterizer = new Rasterizer();
			var bmpd:BitmapData = view.firstLayer.bmp;
			
			var col:uint;
			
			for(i=0; i<objs; i++) {
				
				o = objectList[i];
				if(o.initFrame(f, v, cgv)) continue;
				if(o.initMesh(view, cgv)) continue;
				
				r = o.polygons;
				L = r.length;
				
				o.projectPoints(v, cgv);
				
				for(j=0; j<L; j++) {
					
					p = r[j];
					
					pL = p.vLen;
					if(pL < 2) continue;
					
					vtxs = p.vtxs;
					
					col = FlatAttributes(p.surface.attributes)._color32;
					pL--;
					for(k=0; k<pL; k++) {
						vtxa = vtxs[k];
						vtxb = vtxs[k+1];
						sl.drawLine(vtxa.sx, vtxa.sy, vtxb.sx, vtxb.sy, col, bmpd);
					}
					vtxa = vtxs[k];
					vtxb = vtxs[0];
					sl.drawLine(vtxa.sx, vtxa.sy, vtxb.sx, vtxb.sy, col, bmpd);
				}
			}
			
			view.endFrame(f);
		}
	}
}